package com.xinzhi.yjyz.comtroller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinzhi.entity.Keeper;
import com.xinzhi.entity.Logistics;
import com.xinzhi.util.R;
import com.xinzhi.yjyz.mapper.KeeperMapper;
import com.xinzhi.yjyz.mapper.RetentionMapper;
import com.xinzhi.yjyz.service.IKeeprtService;
import com.xinzhi.yjyz.service.ILogisticsService;
import com.xinzhi.yjyz.service.IRetentionService;
import com.xinzhi.yjyz.service.impl.KeeperServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@ResponseBody
public class KeeperController {
    @Resource
    private IKeeprtService iKeeprtService;
    @Resource
    private KeeperMapper keeperMapper;
    @Resource
    private KeeperServiceImpl keeperService;
    @Resource
    private ILogisticsService iLogisticsService;


    @GetMapping ("/sel")

    public R select() {
        List all = keeperService.findAll();
        System.out.println("jie"+all.toString());
        return new R(200,"成功",all);
    }

    @PostMapping("/selphone/{phone}")
    public R selectphone(@PathVariable("phone") String phone) {
        if(phone!=null&&phone.length()==4){
            List  all=keeperMapper.phoneget("%"+phone);
            return new R(200, "查询成功", all);

        }else {
            return new R(200, "查询失败", "");
       }
    }
    @PostMapping("/selname/{name}")
    public R selectname(@PathVariable("name") String name) {
        if (name!=null){
            List  keepers=keeperMapper.namedet(name);
            return new R(200, "查询成功", keepers);
        }else {
            return new R(200, "查询失败", "");
        }
    }
    @PostMapping("/selnumber/{number}")
    public R selectnumber(@PathVariable String number) {
         if (number!=null){
             List keepers=keeperMapper.numberget("%"+number);
             return new R(200, "查询成功", keepers);
         }else {
             return new R(401,"查询失败","");
         }
    }
    @PostMapping("/insphone/{phone}")
    public R insertphone(@PathVariable("phone") String phone) {
        if (phone!=null&&phone.length()==4) {
            int update = keeperService.upphone(phone);
            return new R(200, "修改成功",update);
        }
        return new R(200, "修改失败","");
    }
    @PostMapping("/insname/{name}")
    public R insertname(@PathVariable("name") String name) {
        if (name!=null){
            System.out.println(name);
            int update= keeperService.upname(name);
            return new R(200, "修改成功", update);
        }else {
            return new R(200, "修改失败", "");
        }

    }
    @PostMapping("/insnumber/{number}")
    public R insertnumber(@PathVariable("number") String number) {
        if (number!=null){
            int update= keeperService.upnumber(number);
            return new R(200, "修改成功", update);
        }else {
            return new R(200, "修改失败", "");
        }
    }

    @PostMapping("/piliang")
    @ResponseBody
    public R piliangtianjia(){
        List<Keeper> list =new ArrayList<>();
        long count=iLogisticsService.count();
        long count1=iKeeprtService.count();
        long count2=count-count1;
        if (count2==0){
            return new R(201,"无需添加新的取件码","");
        }else {
            for (int i = 1; i < count2 + 1; i++) {
                Keeper keeper = new Keeper();
                keeper.setKds_number("1-4-1"+ count1++);
                list.add(keeper);
            }
            boolean b = iKeeprtService.saveBatch(list);
            return new R(200, "批量添加成功", b);
        }
    }
}